// formPackage/pages/newHousePublishModule/newHousePublishThree/newHousePublishThree.js
import { queryNewHouseFormatDetail, createNewHouseFormat, updateNewHouseFormat } from "../../../../service/publish_service"
import { chooseAndUploadQiniuActicon, chooseAndUploadQiniuVideoAction } from "../../../../service/upload_service"
import debounce from "../../../../tools/debounce"
import { VIDEO_URL } from "../../../../service/base_service"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    houseNumber: "",
    joinUrl: VIDEO_URL,
    isShowFloorPopup: false,//户型选择
    showSinglePop: false,//显示选择弹窗
    singlePopType: "",//单项弹窗类型
    singlePopTitle: "选择",//单项弹窗标题
    btnTitle: "户型保存",
    houseTypeItem: {
      "imgUrl": "https://hft.imgs.cdjyh.com/effect/1616490773415-微信图片_20210323165035.jpg",//户型图
      videoUrl: "",//户型视频
      houseTypeName: "",
      singlePrice: "",//单价
      area: "",//面积
      totalPrice: "",//总价
      orientation: "",//朝向
    },
    formData: {
      area: "",//面积
      bedroomNumber: "",//几室
      name: "",//户型名
      orientation: "",//朝向
      totalPrice: "",//总价
      unitPrice: "",//单价
      url: "",//户型图
      videoUrl: "",//视频地址
      vrCoverPic: "",
      vrUrl: "",
      status: "在售",//户型销售状态
      used: "住宅",//户型用途
    },
    formatId: null,//户型详情查询id
    usedArr: [
      {
        name: "住宅",
      },
      {
        name: "公寓",
      },
      {
        name: "商业",
      },
      {
        name: "别墅",
      },
      {
        name: "招商",
      }
    ],
    usedSelectIndex: 0,//默认选中第一个
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      houseNumber: options.houseNumber
    })
    if (options.id) {
      this.setData({
        formatId: options.id
      })
      this.queryNewHouseFormatDetail();
    }
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },

  //选择弹窗弹出
  singleSelectPop(e) {
    let type = e.currentTarget.dataset.type;
    if (type == "name") {
      this.setData({
        isShowFloorPopup: true
      })
    } else if (type == "saleStatus") {
      this.setData({
        singlePopTitle: "销售状态",
        singlePopType: type,
        showSinglePop: true
      })
    }
  },
  //单项输入绑定:
  inputCellChange(e) {
    let type = e.detail.key;
    let changeValue = `formData.${type}`;
    this.setData({
      [changeValue]: e.detail.value
    })
  },
  //选中户型用途
  selectUsedAction(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      usedSelectIndex: index,
      'formData.used': this.data.usedArr[index].name,
    })
  },

  //上传户型图
  pushFormatImg() {
    chooseAndUploadQiniuActicon('format').then(res => {
      console.log("JJLog:图片上传成功:" + JSON.stringify(res));
      wx.showToast({
        title: '上传成功',
      })
      this.setData({
        'formData.url': res.complateUrl
      })
    }).catch(err => {
      console.log("JJLog:图片上传失败:" + JSON.stringify(err));
      wx.showToast({
        title: '图片上传失败',
        icon: "none"
      })
    })
  },

  //上传户型视频
  pushFormatVideo() {
    chooseAndUploadQiniuVideoAction('format').then(imageRes => {
      console.log("JJLog:上传视频成功后返回===:" + JSON.stringify(imageRes));
      this.setData({
        'formData.videoUrl': imageRes.complateUrl,
      })
      wx.showToast({
        title: '上传成功',
        icon: "none"
      })
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: '上传失败',
        icon: "none"
      })
    })
  },

  //选择户型名回调:
  floorPopupSelect(e) {
    // console.log("JJLog:户型回调:" + JSON.stringify(e));
    let result = e.detail.result;
    let resultStr = "";
    result.forEach((item) => {
      if (item.value != "") {
        resultStr = resultStr + item.value;
      }
    })
    this.setData({
      'formData.name': resultStr,
      'formData.bedroomNumber': result[0].value
    })
    this.hidePopup();
  },

  hidePopup() {
    this.setData({
      isShowFloorPopup: false
    })
  },
  //查询一个户型详情:
  queryNewHouseFormatDetail() {
    wx.showLoading();
    queryNewHouseFormatDetail(this.data.formatId).then(res => {
      wx.hideLoading();
      console.log("JJLog:已有户型详情:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          formData: res.data,
          btnTitle: "户型更新"
        })
        this.dealUsed();
      }
    }).catch(err => {
      wx.hideLoading();
      console.error("JJLog:queryNewHouseFormatDetail", err);
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
    })
  },
  //处理户型用途回填:
  dealUsed() {
    if (this.data.formData.used) {
      let arr = this.data.usedArr;
      let index = 0;
      arr.forEach((i, idx) => {
        if (i.name == this.data.formData.used) {
          index = idx;
        }
      })
      this.setData({
        usedSelectIndex: index,
      })
    } else {
      this.setData({
        usedSelectIndex: 0,
        'formData.used': "住宅"
      })
    }
  },


  // 保存这条户型信息;
  pushBasicInfo() {
    this.setData({
      'formData.houseNumber': this.data.houseNumber
    })

    if (this.data.formData.unitPrice) {
      if (!this.checkNumber(this.data.formData.unitPrice)) {
        wx.showToast({
          title: "户型单价只能为数字",
          icon: "none"
        })
        return;
      }
    }

    if (this.data.formData.totalPrice) {
      if (!this.checkNumber(this.data.formData.totalPrice)) {
        wx.showToast({
          title: "户型总价只能为数字",
          icon: "none"
        })
        return;
      }
    }



    if (this.data.formatId) {
      debounce(this.updateInfo);
    } else {
      debounce(this.infoRequest)
    }

  },
  //验证字符串是否是数字
  checkNumber(theObj) {
    let reg = /^[0-9]+.?[0-9]*$/;
    if (reg.test(theObj)) {
      return true;
    }
    return false;
  },
  //上传户型信息:
  infoRequest() {
    wx.showLoading({
      title: '保存中...',
    })
    createNewHouseFormat(this.data.formData).then(res => {
      wx.hideLoading();
      console.log("JJLog:保存一个户型:" + JSON.stringify(res));
      if (res.code == 200) {
        wx.showToast({
          title: '保存成功',
          icon: "success"
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 0,
          })
        }, 2000);
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
    })

  },
  //更新户型信息:
  updateInfo() {
    wx.showLoading({
      title: '保存中...',
    })
    updateNewHouseFormat(this.data.formData).then(res => {
      wx.hideLoading();
      console.log("JJLog:更新一个户型:" + JSON.stringify(res));
      if (res.code == 200) {
        wx.showToast({
          title: '更新成功',
          icon: "success"
        })
        setTimeout(() => {
          wx.navigateBack({
            delta: 0,
          })
        }, 2000);
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
    })
  },

  //单项选择回调
  singlePopSelectCallback(e) {
    console.log('JJLog:单项选择回调:' + JSON.stringify(e));
    let result = e.detail.result;
    this.setData({
      'formData.status': result.value,
      showSinglePop: false
    })
  },

  singlePopHide() {
    this.setData({
      showSinglePop: false
    })
  },



})